package cn.dt70.controller;

import cn.dt70.pojo.AqbProject;
import cn.dt70.pojo.AqbUser;
import cn.dt70.service.AqbProjectService;
import cn.dt70.utils.Constants;
import cn.dt70.utils.PageSupport;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/borrow")
public class BorrowController {

    @Resource
    private AqbProjectService aqbProjectService;

    @RequestMapping("/list")
    public String borrowList(PageSupport ps , HttpSession session , Model model){
        double userAmountSum = 0 ;
        double userInterestSum = 0 ;
        double userPayAmount = 0 ;
        double userPayInterest = 0 ;
        AqbUser aqbUser = (AqbUser) session.getAttribute(Constants.USER_SESSION);
        List<AqbProject> aqbProjects = aqbProjectService.findReturnProjectList(aqbUser.getUserId(),ps.getCurrentPageNo());
        for (AqbProject project: aqbProjects) {
            userAmountSum += project.getProAmount();
            userInterestSum += project.getPayableInterest().setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
            userPayAmount += project.getPayMoneySum().setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
            userPayInterest += project.getPayInterestSum().setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
        }
        model.addAttribute("userAmountSum",userAmountSum);
        model.addAttribute("userInterestSum",userInterestSum);
        model.addAttribute("userPayAmount",userPayAmount);
        model.addAttribute("userPayInterest",userPayInterest);
        model.addAttribute("aqbProjects",aqbProjects);
        model.addAttribute("ps",ps);
        return "usercenter-investcontrol-borrowmoney";
    }

    @RequestMapping("/info")
    public String borrowInfo(PageSupport ps , HttpSession session){

        return "usercenter-investcontrol-paymoney";
    }

}
